package com.hbsx.why.ui.order;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;
import com.hbsx.why.base.BaseFragment;
import com.hbsx.why.model.bean.Modelbean;
import com.hbsx.why.utils.RecyclerViewUtils;
import com.hbsx.why.utils.cache.GlideUtil;
import com.hbsx.why.utils.refresh.PullRecycleView;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.views.BackButton;
import com.hbsx.why.views.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by liub
 * time 2017/9/4
 */

public class OrderFragment extends BaseFragment {

    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.btn_back)
    BackButton btn_back;
    private static String mTitle;
    private static String BUNDLE_flag = "BUNDLE_flag";
    @BindView(R.id.order_PullRecycleView)
    PullRecycleView orderPullRecycleView;
    @BindView(R.id.order_PullScrollView)
    PullScrollView orderPullScrollView;
    @BindView(R.id.order_PullToRefreshLayout)
    PullToRefreshLayout orderPullToRefreshLayout;
    @BindView(R.id.btn_top_right)
    ImageView btnTopRight;
    @BindView(R.id.text_top_right)
    TextView textTopRight;
    @BindView(R.id.top_toolbar)
    FrameLayout topToolbar;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.text5)
    TextView text5;
    Unbinder unbinder;
    private List<Modelbean> modelbeanList;
    private Modelbean modelbean;
    private MyAdapter myAdapter;
    private int page = 0;
    private Context context;
    private boolean isInit = false;

    @Override
    protected int getLayout() {
        return R.layout.order_fragment_main;
    }

    @Override
    protected void onInit() {
        text1.setBackgroundResource(R.color.trans_themecolor);
        context = getActivity();
        tvTopTitle.setText("" + mTitle);
        btn_back.setVisibility(View.GONE);
        orderPullScrollView.setPullDownEnable(true);
        orderPullScrollView.setPullUpEnable(true);
        modelbeanList = new ArrayList<>();
        RecyclerViewUtils.setScrollGridLayout(orderPullRecycleView, context, 1, false);
        myAdapter = new MyAdapter(modelbeanList);
        orderPullRecycleView.setAdapter(myAdapter);
        orderPullRecycleView.addItemDecoration(new SpacesItemDecoration(getContext()));

        orderPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myAdapter.refreshDate(modelbeanList);
                        myAdapter.notifyDataSetChanged();
                        orderPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        myAdapter.addDate(modelbeanList);
                        myAdapter.notifyDataSetChanged();
                        orderPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void fetchData() {
        if (!isInit) {
            isInit = true;
            page = 1;
            getData(page);
            myAdapter.refreshDate(modelbeanList);
            myAdapter.notifyDataSetChanged();
        }
    }

    private void getData(int page) {
        if (page == 1) {
            modelbeanList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                modelbean = new Modelbean();
                if (i % 5 == 0) {
                    modelbean.setName("潜山县鼓书琴书参加安庆市展演");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 1) {
                    modelbean.setName("天天演 天天笑 天天乐");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 2) {
                    modelbean.setName("彭保友民俗花钱收藏展");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 3) {
                    modelbean.setName("第三届相声大会");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 4) {
                    modelbean.setName("姚道馀写生作品展");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }

            }
        } else {
            modelbeanList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                if (i % 5 == 0) {
                    modelbean.setName("“猴年说猴”趣味故事会");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 1) {
                    modelbean.setName("文化惠民春潮涌");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 2) {
                    modelbean.setName("市书画院精心打造“廉政文化长廊”");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 3) {
                    modelbean.setName("书画艺术进军营，军民共叙强军梦");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
                if (i % 5 == 4) {
                    modelbean.setName("新火车站笔友会");
                    modelbean.setJuli("" + 100);
                    modelbeanList.add(modelbean);
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void refreshList() {
        page = 1;
        getData(page);
        myAdapter.refreshDate(modelbeanList);
        myAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.text1, R.id.text2, R.id.text3, R.id.text4, R.id.text5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text1:
                clearBg(0);
                text1.setBackgroundResource(R.color.trans_themecolor);
                refreshList();
                break;
            case R.id.text2:
                clearBg(1);
                text2.setBackgroundResource(R.color.trans_themecolor);
                refreshList();
                break;
            case R.id.text3:
                clearBg(2);
                text3.setBackgroundResource(R.color.trans_themecolor);
                refreshList();
                break;
            case R.id.text4:
                clearBg(3);
                text4.setBackgroundResource(R.color.trans_themecolor);
                refreshList();
                break;
            case R.id.text5:
                clearBg(4);
                text5.setBackgroundResource(R.color.trans_themecolor);
                refreshList();
                break;
        }
    }

    public void clearBg(int classify) {
        flag = classify;
        text1.setBackgroundResource(R.color.white);
        text2.setBackgroundResource(R.color.white);
        text3.setBackgroundResource(R.color.white);
        text4.setBackgroundResource(R.color.white);
        text5.setBackgroundResource(R.color.white);
    }

    private int flag = 0;

    public class MyAdapter extends LGRecycleViewAdapter<Modelbean> {
        public MyAdapter(List<Modelbean> dataList) {
            super(dataList);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.layout_order_item;
        }

        @Override
        public void convert(LGViewHolder holder, Modelbean modelbean, int position) {
            TextView title = (TextView) holder.getView(R.id.title);
            ImageView img = (ImageView) holder.getView(R.id.img_head);
            title.setText(modelbean.getName());
            if (flag == 0) {
                if (position % 3 == 0) {
                    GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.demo_img_book);
                }
                if (position % 3 == 1) {
                    GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.home_dkx_headimg);
                }
                if (position % 3 == 2) {
                    GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.image_icon);
                }

            }
            if (flag == 1) {
                GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.home_dkx_headimg);
            }
            if (flag == 2) {
                GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.image_icon);
            }
            if (flag == 3) {
                GlideUtil.getInstance().loadLocationImage(context, img, R.mipmap.demo_img_book);
            }
        }
    }

    public static OrderFragment newInstance(String title, int flag) {
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_flag, flag);
        mTitle = title;
        OrderFragment fragment = new OrderFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
